package com.weilele.base.library

import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityOptionsCompat
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentContainerView
import com.weilele.mvvm.base.MvvmActivity
import com.weilele.mvvm.base.MvvmFragment
import com.weilele.mvvm.utils.activity.navigateTo
import com.weilele.mvvm.utils.fragment.addFragment
import com.weilele.mvvm.utils.result_contract.navigateForResult
import com.weilele.mvvm.utils.safeGet

/**
 * 包含fragment的activity
 */
open class FragmentContainerActivity : BaseActivity() {
    companion object {
        const val KEY_FRAGMENT_CLASS_NAME = "KEY_FRAGMENT_CLASS_NAME"
        const val KEY_FRAGMENT_BUNDLE = "KEY_FRAGMENT_BUNDLE"

        /**
         * 在activity启动数据
         */
        inline fun <reified T : Fragment> start(
            appCompatActivity: AppCompatActivity,
            bundle: Bundle? = null,/*此数据将会传递给fragment*/
            option: ActivityOptionsCompat? = null,
        ) {
            appCompatActivity.navigateTo<FragmentContainerActivity>(option = option) {
                putData<T>(it, bundle)
            }
        }

        /**
         * 在activity启动数据
         */
        inline fun <reified T : Fragment> startForResult(
            appCompatActivity: MvvmActivity,
            bundle: Bundle? = null,
            option: ActivityOptionsCompat? = null,
            noinline onResult: (resultCode: Int, resultData: Intent?) -> Unit
        ) {
            appCompatActivity.navigateForResult<FragmentContainerActivity>(
                option = option,
                setParams = {
                    putData<T>(it, bundle)
                },
                onResult = onResult
            )
        }

        /**
         * 在fragment启动activity
         */
        inline fun <reified T : Fragment> start(
            fragment: Fragment,
            bundle: Bundle? = null,
            option: ActivityOptionsCompat? = null,
        ) {
            fragment.navigateTo<FragmentContainerActivity>(option = option) {
                putData<T>(it, bundle)
            }
        }

        inline fun <reified T : Fragment> startForResult(
            fragment: MvvmFragment,
            bundle: Bundle? = null,
            option: ActivityOptionsCompat? = null,
            noinline onResult: (resultCode: Int, resultData: Intent?) -> Unit
        ) {
            fragment.navigateForResult<FragmentContainerActivity>(
                option = option,
                setParams = {
                    putData<T>(it, bundle)
                },
                onResult = onResult
            )
        }

        /**
         * 设置携带数据
         */
        inline fun <reified T : Fragment> putData(intent: Intent, bundle: Bundle? = null) {
            intent.putExtra(KEY_FRAGMENT_CLASS_NAME, T::class.java)
            bundle?.let {
                intent.putExtra(KEY_FRAGMENT_BUNDLE, bundle)
            }
        }
    }

    override fun onMvvmCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): Any? {
        return FragmentContainerView(this).apply {
            id = R.id.base_library_fragment_container_id
            intent.getSerializableExtra(KEY_FRAGMENT_CLASS_NAME).safeGet<Class<Fragment>>()?.let {
                addFragment(id, it.newInstance().apply {
                    arguments = intent.getBundleExtra(KEY_FRAGMENT_BUNDLE)
                })
            }
        }
    }
}